<?php


namespace App\Admin\Extensions\Form\Recommend;

use App\Http\Traits\GlobalConstant;
use App\Models\Campus;
use App\Models\Recommend;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class Edit extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '推荐课程互动';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {

        try {
            $aFiles = [];
            if (!empty($request->file())) {

                $aFileFields = ['discover', 'video_url'];
                foreach ($aFileFields as $fileField) {
                    $fileHandle   = $request->file($fileField);
                    $default_disk = config('admin.upload.disk');
                    if ($fileHandle) {
                        if (!is_array($fileHandle)) {
//                        $store_result = $fileHandle->store('adm_files');
                            $store_result = $fileHandle->store(config('admin.upload.directory.file'));
                            if (!$store_result) {
                                throw new \Exception('上传失败1');
                            }
                            $fileUrl            = config('filesystems.disks.' . $default_disk . '.url') . $store_result;
                            $aFiles[$fileField] = $fileUrl;
                        } else {
                            foreach ($fileHandle as $value) {
//                            $store_result = $value->store('adm_files');
                                $store_result = $value->store(config('admin.upload.directory.file'));
                                if (!$store_result) {
                                    throw new \Exception('上传失败2');
                                }
                                $fileUrl = config('filesystems.disks.' . $default_disk . '.url') . $store_result;
                                $arr[]   = $fileUrl;

                            }
                            $aFiles[$fileField] = json_encode($arr);
                        }

                    }
                }
            }

            //合并post和文件
            $inputs = array_merge($request->input(), $aFiles);
            unset($inputs['s']);
//            Log::info('提交的数据',$inputs);
            if (!empty($inputs['id'])) {
                $id = $inputs['id'];
                unset($inputs['id']);
                Recommend::where('id', $id)->update($inputs);
            } else {

                Recommend::create($inputs);
            }
            admin_success('保存成功');
            return redirect('/admin/recommend');

        } catch (\Exception $exception) {
            Log::error('失败了' . $exception->getMessage());
            admin_error('保存失败');
            return back();
        }
    }

    public function form()
    {
        $recommendId   = request('r_id') ?? 0;
        $recommendInfo = Recommend::where('id', $recommendId)->first();
        $this->hidden('id', 'id')->default($recommendId);
        $this->text('title', '标题')->required()->attribute(['autocomplete' => 'off'])->default($recommendInfo['title']);
        $this->select('campus_id', '校区')->options(Campus::where('status', 1)->pluck('name as text', 'id'))->required()->default($recommendInfo['campus_id']);
        $this->image('discover', '封面')->required()->uniqueName();
        $this->file('video_url', '视频')->uniqueName()->required();
        $this->text('intro', '视频标签文字')->required()->default($recommendInfo['intro']);
        $this->number('hot', '热度')->min(0)->default($recommendInfo['hot'] ?? 0);
        $this->radio('status', '状态')->options(GlobalConstant::_STATUS)->default($recommendInfo['status'] ?? 1);
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $id   = request('r_id') ?? 0;
        $info = Recommend::find($id);
        if (!$info) {
            return [];
        }
        return [
            'discover'  => $info->discover,
            'video_url' => $info->video_url,
        ];
    }

}
